/*
Content: Creates main outcomes exploiting the panel dimension of data
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
*** LOAD DATASET from do-file "1_*"
********************************************************************************

clear
use "$intermediate/40perc_full_noger_prepared"


* We want to fix the fact that some employment spells have valid ieb_beg_alo and substitute with missing 
bys persnr (begepi): replace ieb_beg_alo = . if erwstat == 101 & erwstat[_n-1] == 101 & betnr == betnr[_n-1]  
bys persnr (begepi): replace ieb_beg_alo = . if erwstat == 101 & erwstat[_n+1] == 101 & betnr == betnr[_n+1]  


********************************************************************************
*** DATES 
********************************************************************************

* Retrieve date of first new employment if spells are not employment
cap drop end_last_emp
gen end_last_emp = .
gen start_next_emp =. 


forvalues i = 1/200 {
bys persnr (begepi): replace start_next_emp = begepi[_n+`i'] if ieb_beg_alo != . & ieb_beg_alo[_n+`i'] == . & erwstat[_n+`i'] == 101 & start_next_emp == . 
	
bys persnr (begepi): replace start_next_emp = begepi[_n+`i'] if ieb_beg_alo == . & quelle != 1 & ieb_beg_alo[_n+`i'] == . & erwstat[_n+`i'] == 101 & start_next_emp == . 	
}

format start_next_emp end_last_emp %td


* Retrieve date of last employment and first new employment if spells are employment  (erwstat = 101) 
cap gen erwstat101 = 1 if erwstat == 101 & ieb_beg_alo == . // I want to exclude all those very short spells that are in between registered unemployment spells
replace erwstat101 = 0 if erwstat101 != 1


bys persnr erwstat101 (begepi): replace end_last_emp = endepi[_n-1]  if erwstat101 == 1
bys persnr erwstat101 (begepi): replace start_next_emp = begepi[_n+1]  if erwstat101 == 1


sort persnr begepi


* Retrieve next date of employment registration if spell is employment	
cap drop end_last_alo
cap drop start_next_alo 

gen end_last_alo = .
gen start_next_alo =. 


forvalues i = 1/300 {
	
	bys persnr (begepi): replace start_next_alo = ieb_beg_alo[_n+`i'] if ieb_beg_alo[_n+`i'] !=. & erwstat == 101 & quelle == 1 & start_next_alo == . & ieb_beg_alo[_n+`i'] > endepi
	
}

format start_next_alo end_last_alo %td


********************************************************************************
*** CHARACTERIZE SPELLS IN BETWEEN EMPLOYMENT SPELLS
********************************************************************************

* Characterize the spells inbetween employment spells 

	/* 
	- days of job search
	- days of paid benefits 
	- days of azubi 
	- days of geringfuegig
			
	* Procedure: I first assign to the employment spell the begepi of the next employment spell. I then carryforward all the spells in between that are not erwstat == 101. I then give a 1 to all these in between spells if they are registered unemployment spells (ieb_beg_alo !=.). I maximize to all spells with the same next employment spells (this includes the last employment spell). I tab
	*/
	

* Compute duration of individual spells 
	cap gen dauer = endepi-begepi
	
* Number of days in job search 
	cap drop x 
	gen x = dauer if ieb_beg_alo != . & erwstat != 101
	bys persnr start_next_emp (begepi): egen tot_days_jobsearch = sum(x)



********************************************************************************
**# PART-TIME
********************************************************************************	

cap drop teilzeit_v2

recode berufstellg_imp_num (8 9 21 = 1 "parttime") (0 1 2 3 4 5 6 7 20 = 0 "fulltime") if berufstellg_imp_num >= 0, gen (teilzeit_v2)


********************************************************************************
**# KEEP ONLY USEFUL VARIABLES 
********************************************************************************	

	keep persnr dauer ieb_beg_alo quelle erwstat tot* tentgelt begepi endepi end* start* betnr *beruf* *wz* *wo* *ao* geb_dat nat* *staat* erwstat101 persnr *spell edu* age* teilzeit* geb* frau *dau*
	
	
	
********************************************************************************
**# KEEP ONLY EMPLOYMENT SPELLS WITH NO OVERLAPPING WITH UNEMPLOYMENT SPELLS (ieb_beg_alo ==.)
********************************************************************************	

keep if erwstat == 101 & ieb_beg_alo == . 
	
	
********************************************************************************
*** ATTACH FIRMS PANEL INFO - BHP dataset
********************************************************************************

global fileBHP "\\IAB\dfs\017\Ablagen\D01700-IAB-Projekte\D01700-Projekte-FDZ\Datensaetze\_Endprodukte\BHP\BHP7522_v1\Grundgesamtheit"

* observation period
global minYear = 1997 // begin of your observation period		(default: 1975)
global maxYear = 2019	// end of your observation period		(default: 2014)


cap drop _bhp*

cap gen jahr = year(begepi)

forvalues jahr = 1997/2019 {
	
preserve 

use $fileBHP/bhp_7522_m06_v1_`jahr'.dta, clear 

tempfile bhp`jahr'
gen jahr = `jahr'

save `bhp`jahr''

restore

merge m:1 betnr jahr using `bhp`jahr'', gen(_bhp`jahr') keepusing(betnr grd_dat lzt_dat az_ges az_f az_gq az_mq az_hq az_d alter_mw az_vz az_tz az_f_vz az_f_tz az_d az_eu te_imp_med te_imp_p25 te_imp_p75) update

drop if _bhp`jahr' == 2

	
erase `bhp`jahr''

}



compress

save "$intermediate/employmentspells.dta", replace

}
	

	
